package com.wwl.springcloud.controller;

import com.wwl.springcloud.dto.CommonResult;
import com.wwl.springcloud.entity.Storage;
import com.wwl.springcloud.repository.StorageRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/23 20:28
 * @description:
 */
@RestController
@Slf4j
@Api(value = "/storage", description = "库存", tags = {"storage"})
public class StorageController {
    private CommonResult commonResult = CommonResult.getInstance();
    @Autowired
    private StorageRepository storageRepository;



    /**
     * 扣减库存信息
     */
    @ApiOperation("扣减库存")
    @PostMapping("/decrease")
    public CommonResult decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count){
        Storage storage=storageRepository.findByProductId(productId);
        if (storage!=null){
            if (storage.getResidue()>0){
                int a = 10/0;
                log.info("-------> 开始扣减库存,productId:{},count:{}",productId,count);
                storage.setResidue(storage.getResidue()-count);
                storage.setUsed(storage.getUsed()+count);
                storageRepository.save(storage);
                log.info("-------> 扣减库存结束");
                return commonResult.success("扣减成功！");
            }else{
                return commonResult.success("没库存了！");
            }

        }
        return commonResult.error("扣减失败");
    }



}
